package repository

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
)

type repoLocalCompBuilder struct {
}

func (e *repoLocalCompBuilder) BuildCompEntity(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	if compRepo == "local" && compType == "jar-decoder" {
		return e.buildJarDecoder(params)
	}
	if compRepo == "local" && compType == "jsp-decoder" {
		return e.buildJspDecoder(params)
	}
	if compRepo == "local" && compType == "jsn-decoder" {
		return e.buildJsnDecoder(params)
	}
	if compRepo == "local" && compType == "app-service" {
		return e.buildAppService(params)
	}
	if compRepo == "local" && compType == "device-template" {
		return e.buildDevTemplate(params)
	}
	if compRepo == "local" && compType == "iot-template" {
		return e.buildIotTemplate(params)
	}

	return nil, errors.New("不支持的类型:" + compRepo + "," + compType)
}

func (e *repoLocalCompBuilder) buildJarDecoder(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	manufacturer := Map.GetString(compParam, "manufacturer", "")
	deviceType := Map.GetString(compParam, "deviceType", "")
	fileName := Map.GetString(compParam, "fileName", "")
	if Method.HasNull(manufacturer, deviceType, fileName) {
		return nil, errors.New("参数不能为空: manufacturer, deviceType, fileName")
	}

	// 构造作为参数的实体
	entity := edgeEntity.Type.RepoCompEntity.Instance()
	entity.CompRepo = compRepo
	entity.CompType = compType
	entity.CompName = fileName
	entity.CompParam = compParam

	return entity, nil
}

func (e *repoLocalCompBuilder) buildJspDecoder(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	manufacturer := Map.GetString(compParam, "manufacturer", "")
	deviceType := Map.GetString(compParam, "deviceType", "")
	if Method.HasNull(manufacturer, deviceType) {
		return nil, errors.New("参数不能为空: manufacturer, deviceType")
	}

	// 构造作为参数的实体
	entity := edgeEntity.Type.RepoCompEntity.Instance()
	entity.CompRepo = compRepo
	entity.CompType = compType
	entity.CompName = manufacturer + ":" + deviceType
	entity.CompParam = compParam

	return entity, nil
}

func (e *repoLocalCompBuilder) buildJsnDecoder(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	manufacturer := Map.GetString(compParam, "manufacturer", "")
	deviceType := Map.GetString(compParam, "deviceType", "")
	if Method.HasNull(manufacturer, deviceType) {
		return nil, errors.New("参数不能为空: manufacturer, deviceType")
	}

	// 构造作为参数的实体
	entity := edgeEntity.Type.RepoCompEntity.Instance()
	entity.CompRepo = compRepo
	entity.CompType = compType
	entity.CompName = manufacturer + ":" + deviceType
	entity.CompParam = compParam

	return entity, nil
}

func (e *repoLocalCompBuilder) buildDevTemplate(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	manufacturer := Map.GetString(compParam, "manufacturer", "")
	deviceType := Map.GetString(compParam, "deviceType", "")
	subsetName := Map.GetString(compParam, "subsetName", "")
	if Method.HasNull(manufacturer, deviceType, subsetName) {
		return nil, errors.New("参数不能为空: manufacturer, deviceType, subsetName")
	}

	// 构造作为参数的实体
	entity := edgeEntity.Type.RepoCompEntity.Instance()
	entity.CompRepo = compRepo
	entity.CompType = compType
	entity.CompName = manufacturer + ":" + deviceType + ":" + subsetName
	entity.CompParam = compParam

	return entity, nil
}

func (e *repoLocalCompBuilder) buildIotTemplate(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	iotName := Map.GetString(compParam, "iotName", "")
	subsetName := Map.GetString(compParam, "subsetName", "")
	if Method.HasNull(iotName, subsetName) {
		return nil, errors.New("参数不能为空: iotName, subsetName")
	}

	// 构造作为参数的实体
	entity := edgeEntity.Type.RepoCompEntity.Instance()
	entity.CompRepo = compRepo
	entity.CompType = compType
	entity.CompName = iotName + ":" + subsetName
	entity.CompParam = compParam

	return entity, nil
}

func (e *repoLocalCompBuilder) buildAppService(params map[string]interface{}) (*edgeEntity.RepoCompEntity, error) {
	// 提取业务参数
	compRepo := Map.GetString(params, "compRepo", "")
	compType := Map.GetString(params, "compType", "")
	compParam := Map.GetMap(params, "compParam", make(map[string]interface{}))

	// 简单校验参数
	if Method.HasNull(compType, compRepo, compParam) {
		return nil, errors.New("参数不能为空: compType, compRepo, compParam")
	}

	appName := Map.GetString(compParam, "appName", "")
	appType := Map.GetString(compParam, "appType", "")
	if Method.HasNull(appName, appType) {
		return nil, errors.New("参数不能为空: appName, appType")
	}

	// 构造作为参数的实体
	entity := edgeEntity.Type.RepoCompEntity.Instance()
	entity.CompRepo = compRepo
	entity.CompType = compType
	entity.CompName = appType + ":" + appName
	entity.CompParam = compParam

	return entity, nil
}
